package route

import (
	"github.com/gin-gonic/gin"
	swaggerFiles "github.com/swaggo/files"
	ginSwagger "github.com/swaggo/gin-swagger"
	"net/http"
	"wish_go/src/main/com/ajie/controller"
	"wish_go/src/main/com/ajie/filter"
)

func CollectRoute(r *gin.Engine) *gin.Engine {
	r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
	/* 白名单为上 */
	// 登陆
	r.POST("/user/login", controller.Login)
	// 获取当前用户信息
	r.GET("/user/getInfo", filter.AuthFilter(), controller.GetInfo)
	r.POST("/user/page", filter.AuthFilter(), controller.UserList)
	r.POST("/user/setRole", filter.AuthFilter(), controller.SetRole)
	r.POST("/user/resetPassword", filter.AuthFilter(), controller.ResetPassword)
	// 创建验证码
	r.GET("/captcha/create", controller.GenerateImg)
	// 获取验证base64
	r.GET("/captcha/img/:key", controller.GetCaptcha)
	// 文件上传
	r.POST("/upload/image", controller.UploadImage)
	// 编辑器图片上传 需鉴权
	r.POST("/editor/upload/image", filter.AuthFilter(), controller.EditorImage)

	// 轮播图
	r.POST("/carousel/page", filter.AuthFilter(), controller.PageCarousel)
	// 获取轮播图，不需要登陆
	r.GET("/carousel/index", controller.Carousel)
	r.POST("/carousel/insert", filter.AuthFilter(), controller.InsertCarousel)
	r.PUT("/carousel/update", filter.AuthFilter(), controller.UpdateCarousel)
	r.DELETE("/carousel/delete/:id", filter.AuthFilter(), controller.DeleteCarousel)
	// 社区活动
	r.POST("/activity/page", controller.PageActivity)
	r.POST("/activity/insert", filter.AuthFilter(), controller.InsertActivity)
	r.PUT("/activity/update", filter.AuthFilter(), controller.UpdateActivity)
	r.DELETE("/activity/delete/:id", filter.AuthFilter(), controller.DeleteActivity)
	// 社区管理
	r.POST("/area/tree", filter.AuthFilter(), controller.TreeArea)
	r.POST("/area/insert", filter.AuthFilter(), controller.InsertArea)
	r.PUT("/area/update", filter.AuthFilter(), controller.UpdateArea)
	r.DELETE("/area/delete/:id", filter.AuthFilter(), controller.DeleteArea)

	r.GET("/gen/code", controller.GenCode)
	r.GET("/batch/gen/code", controller.AllAreaQrCode)

	// 微信小程序接口
	r.GET("/mini/login", controller.MiniLogin)
	r.GET("/mini/getInfo/:openid", controller.GetUserInfo)
	r.POST("/mini/user/update", controller.UpdateUserInfo)

	// 心愿
	r.POST("/wish/page", controller.PageWish)
	r.POST("/wish/insert", controller.InsertWish)
	r.POST("/wish/update", controller.UpdateWish)
	r.POST("/wish/hotspot", filter.AuthFilter(), controller.WishHotspot)
	r.GET("/wish/delete", controller.DeleteWish)
	r.GET("/wish/area/:openid", controller.GetAreaByOpenid)
	// 资源未找到
	r.NoMethod(func(ctx *gin.Context) {
		ctx.JSON(http.StatusMethodNotAllowed, gin.H{"code": 405, "message": "不被允许的请求"})
	})
	r.NoRoute(func(ctx *gin.Context) {
		ctx.JSON(http.StatusNotFound, gin.H{"code": 404, "message": "资源未找到"})
	})
	return r
}

func Cors() gin.HandlerFunc {
	return func(context *gin.Context) {
		method := context.Request.Method
		context.Header("Access-Control-Allow-Origin", "*")
		context.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization, Token, Content-Language, city")
		context.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, PATCH, PUT")
		context.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type, refreshToken")
		context.Header("Access-Control-Allow-Credentials", "true")
		if method == "OPTIONS" {
			context.AbortWithStatus(http.StatusNoContent)
		}
		context.Next()
	}
}
